
*************************************************************
*Appendix Table 5. Factors associated with attrition patterns
************************************************************* 

version 15.1
cd "${mypath}\CHNS_project\01_data\02_posted\"

log using "${mypath}\CHNS_project\03_log_files\10_appendix_table_5.log", replace

use "CHNS_1993_2015_unimputed.dta",clear

*generate exclusion condition, that is, people who work in agriculture in urban area
gen be_farmer = (migrant_group != 2 & occupation == 3 & work_unit == 4) if be_employed == 1

*generate respondents' acceptance of the survey in analytical data
drop if be_farmer == 1 & (be_migrant == 1 | be_urban_resident == 1)
sort wave IDind
*generate the number of surveys for each individual
gen n = 1
bys IDind:egen n_survey = sum(n)
*generate the sequence of surveys for each individual
bys IDind:gen serial_survey = _n
*generate the indicator of last survey for each individual
bys IDind:egen max_survey = max(serial_survey)
*generate the indicator of previous survey for each individual
gen prior_survey = max_survey - 1

*construct two types indicator that reflect the pattern of survey response provided by each individual respondent
*(1)once: indicator of whether an individual only appears once
gen once = n_survey == 1
*(2)prop: indicator of proportion of waves responded 
gen prop = n_survey/8

*assign predictors to the local macro
local covars "gender ethnicity age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 year_education be_rural_resident be_migrant num_child num_elderly h_group1 h_group2 h_group4 h_group5 log_pc_income HAI year1997 year2000 year2004 year2006 year2009 year2011 year2015 year1997##be_migrant year2000##be_migrant year2004##be_migrant year2006##be_migrant year2009##be_migrant year2011##be_migrant year2015##be_migrant Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou"

local path "${mypath}\CHNS_project\04_tables\appendix_table_5\"

*generate predictor variables of attrition status
*(1)indicator of whether an individual only appears once
reg once `covars' if serial_survey == 1,noomitted
est store once_entry,title(once_entry)
reg prop `covars' if serial_survey == 1,noomitted
est store prop_entry,title(prop_entry)

*(2)indicator of proportion of waves responded 
gen be_prior = 1 if prior_survey == serial_survey
replace be_prior = 1 if prior_survey == 0

reg once `covars' if be_prior == 1,noomitted
est store once_prior,title(once_prior)
reg prop `covars' if be_prior == 1,noomitted
est store prop_prior,title(prop_prior)

esttab once_entry once_prior prop_entry prop_prior using "`path'appendix_table_5.csv", constant b(3) star(+ 0.10 * 0.05 ** 0.01) se(%9.3f) r2 replace nogap nonumbers noomitted stats(N)

*descriptive statistics for attrition outcomes
sum once if serial_survey == 1 & be_migrant == 1
sum once if serial_survey == 1 & be_urban_resident == 1
sum prop if be_prior == 1 & be_migrant == 1
sum prop if be_prior == 1 & be_urban_resident == 1

log close
